package archiving.gui;

import gui.util.layout.StandardForm;

import javax.swing.*;

import java.awt.*;
import java.io.File;
import java.util.Collection;

import archiving.logic.IOpenZipArchiver;

/**
 * This is an implementation of <code>IArchiverUI</code> interface.<br>
 * It provides a graphical user interface for the archiving logic. For seeing
 * how the GUI looks like, refer to the specification.
 * 
 * @author Jakub Závěrka
 */
public class ArchiverGUI implements IArchiverUI
{
	private JFrame mainWindow;
	private JTree fileTree;
	private FileCheckList folderList;
	private JList fileList;
	private JButton addFileButton;
	private JButton removeFileButton;
	private JButton closeButton;
	private JButton makeArchiveButton;

	/**
	 * This will create a new instance of the GUI. Should be called only once.
	 * Preferred method to display the GUI is the <code>showGui</code> method.
	 */
	public ArchiverGUI(){
		prepareComponents();
	}

	/**
	 * This will prepare the graphical components and layout them in their
	 * respective containers.
	 */
	private void prepareComponents(){
		prepareMainWindow();
		FilePanel filePanel = new FilePanel();
		filePanel.getJComponent().setBorder(
					BorderFactory.createEmptyBorder(0, 0, 5, 0));
		StandardForm form = new StandardForm(filePanel.getJComponent(),
								prepareCenterPanel(), null);
		form.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

		mainWindow.setContentPane(form.getContainer());
		mainWindow.pack();
		mainWindow.setLocationRelativeTo(null);
	}

	/**
	 * This will prepare the center panel and its components. Gets called by
	 * <code>prepareComponents()</code>.
	 */
	private Component prepareCenterPanel(){
		fileTree = new JTree();
		fileTree.setPreferredSize(new Dimension(200, 500));
		folderList = new FileCheckList();
		folderList.setPreferredSize(new Dimension(200, 500));

		Box b = new Box(BoxLayout.X_AXIS);

		JPanel left = new JPanel(new BorderLayout());
		left.add(new JLabel("Folders:"), BorderLayout.NORTH);
		left.add(new JScrollPane(fileTree));
		b.add(left);

		b.add(Box.createRigidArea(new Dimension(5, 0)));

		JPanel center = new JPanel(new BorderLayout());
		center.add(new JLabel("Files:"), BorderLayout.NORTH);
		center.add(new JScrollPane(folderList));
		b.add(center);

		b.add(Box.createRigidArea(new Dimension(5, 0)));

		JPanel right = new JPanel(new BorderLayout());
		right.add(new JLabel("Number of files: 0"), BorderLayout.NORTH);
		right.add(prepareListPanel());
		b.add(right);

		b.setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0));
		return b;
	}

	/**
	 * This will prepare panel with the file list and its components. Gets called
	 * by <code>prepareCenterPanel()</code>.
	 */
	private Component prepareListPanel(){
		fileList = new JList();
		addFileButton = new JButton("Add a file...");
		removeFileButton = new JButton("Remove file");
		closeButton = new JButton("Close");
		makeArchiveButton = new JButton(
					"<html><font size='4'><b>Make archive</b></font></html>");
		makeArchiveButton.setPreferredSize(new Dimension(130, 50));

		JPanel fileListPanel = new JPanel(new BorderLayout());
		fileListPanel.add(new JScrollPane(fileList));
		Box fileButtonPanel = new Box(BoxLayout.X_AXIS);
		fileButtonPanel.add(addFileButton);
		fileButtonPanel.add(Box.createRigidArea(new Dimension(5, 0)));
		fileButtonPanel.add(removeFileButton);
		fileButtonPanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0));
		fileListPanel.add(fileButtonPanel, BorderLayout.SOUTH);

		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		buttonPanel.add(closeButton);
		buttonPanel.add(Box.createRigidArea(new Dimension(10, 0)));
		buttonPanel.add(makeArchiveButton);
		buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0));

		StandardForm rightForm =
					new StandardForm(null, fileListPanel, buttonPanel);

		return rightForm.getContainer();
	}

	/**
	 * This will initialize the main window of the interface. gets called by the
	 * <code>prepareComponents()</code> method.
	 */
	private void prepareMainWindow(){
		mainWindow = new JFrame("OpenZIP Archiver");
		mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	/**
	 * {@inheritDoc} <br>
	 * This implementaion will display an empty environment.
	 */
	@Override public void useUI(IOpenZipArchiver logic){
		mainWindow.setVisible(true);
	}

	/**
	 * {@inheritDoc} <br>
	 * This implementaion will display the environment with specified files
	 * pre-selected. Changes will be reflected in the tree, folder list and file
	 * list. Other files may get selected and the pre-selected files amy get
	 * de-selected.
	 */
	@Override public void useUI(IOpenZipArchiver logic, Collection<File> files){
		throw new UnsupportedOperationException("Not yet implemented.");
	}

	/**
	 * Testing method.
	 */
	public static void main(String[] args){
		/*try{
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}
		catch(ClassNotFoundException e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch(InstantiationException e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch(IllegalAccessException e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch(UnsupportedLookAndFeelException e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		ArchiverGUI gui = new ArchiverGUI();
		gui.useUI(null);
	}
}
